﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BenzInfo.Hockey.Grenadiers.Models;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Data.Entity.Infrastructure;

namespace BenzInfo.Hockey.Grenadiers.Controllers
{ 
    public class EventsController : BaseController
    {
        private GrenadiersContext db = new GrenadiersContext();

        //
        // GET: /Events/

        public ViewResult Index()
        {
            return View(db.Events.ToList());
        }

        public ActionResult Qtip(int id)
        {
            Event evnt = db.Events.Find(id);
            return PartialView(evnt);
        }

        //
        // GET: /Events/Create

        public ActionResult Create()
        {
            return PartialView();
        } 

        //
        // POST: /Events/Create

        [HttpPost]
        public ActionResult Create(Event evnt)
        {
            evnt.UserCreatedBy = ViewBag.CurrentUser;
            evnt.DateCreated = DateTime.Now;
            evnt.UserModifiedBy = ViewBag.CurrentUser;
            evnt.DateModified = DateTime.Now;

            ModelState.Remove("UserCreatedBy");
            ModelState.Remove("UserModifiedBy");
            
            if (ModelState.IsValid)
            {
                db.Events.Attach(evnt);
                db.Events.Add(evnt);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return PartialView(evnt);
        }
        
        //
        // GET: /Events/Edit/5
 
        public ActionResult Edit(int id)
        {
            Event evnt = db.Events.Find(id);
            return PartialView(evnt);
        }

        //
        // POST: /Events/Edit/5

        [HttpPost]
        public ActionResult Edit(Event evnt)
        {
            Event evntdb = db.Events.Find(evnt.Id);

            evnt.UserModifiedBy = ViewBag.CurrentUser;
            evnt.DateModified = DateTime.Now;

            ModelState.Remove("UserCreatedBy");
            ModelState.Remove("UserModifiedBy");

            TryUpdateModel(evntdb);

            if (ModelState.IsValid)
            {
                db.Events.Attach(evntdb);
                db.Entry(evntdb).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return PartialView(evnt);
        }

        //
        // GET: /Events/Delete/5
 
        public ActionResult Delete(int id)
        {
            Event evnt = db.Events.Find(id);
            return PartialView(evnt);
        }

        //
        // POST: /Events/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Event evnt = db.Events.Find(id);
            db.Events.Remove(evnt);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}